package com.hpf.community.shiro;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.hpf.community.constants.CommunityConstants;
import com.hpf.community.model.RoleAuthModel;
import com.hpf.community.model.UserModel;
import com.hpf.community.service.IRoleAuthService;
import com.hpf.community.service.IUserService;
import org.apache.shiro.authc.AuthenticationException;
import org.apache.shiro.authc.AuthenticationInfo;
import org.apache.shiro.authc.AuthenticationToken;
import org.apache.shiro.authc.SimpleAuthenticationInfo;
import org.apache.shiro.authz.AuthorizationInfo;
import org.apache.shiro.authz.SimpleAuthorizationInfo;
import org.apache.shiro.realm.AuthorizingRealm;
import org.apache.shiro.subject.PrincipalCollection;
import org.apache.shiro.util.ByteSource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * @Description:
 * @Author: hpf
 * @Date: 2020/4/17 17:09
 */
public class CommunityRealm extends AuthorizingRealm {

    private static final transient Logger logger = LoggerFactory.getLogger(CommunityRealm.class);

    @Autowired
    public IUserService userService;

    @Autowired
    public IRoleAuthService roleAuthService;

    @Override
    protected AuthorizationInfo doGetAuthorizationInfo(PrincipalCollection principals) {
        logger.info("------------进入授权--------");
        SimpleAuthorizationInfo authorizationInfo = new SimpleAuthorizationInfo();
        String username = (String) principals.getPrimaryPrincipal();
        Wrapper<UserModel> wrapper = new EntityWrapper<>();
        wrapper.eq(UserModel.NAME,username);
//        wrapper.or();
//        wrapper.eq(UserModel.EMAIL,username);
        UserModel user = userService.selectOne(wrapper);
        List<RoleAuthModel> authModels = roleAuthService.selectRoleAuthList(user.getRole());
        //1：取出当前用户所拥有的角色
        //2: 将角色添加进权限信息中
        //3: 添加角色对应的权限名称
        authorizationInfo.addRole(user.getRole());//获取角色
        //权限
        for(RoleAuthModel obj:authModels){
            authorizationInfo.addStringPermission(obj.getAuthName());//加入权限名称
        }

        return authorizationInfo;
    }

    @Override

    protected AuthenticationInfo doGetAuthenticationInfo(AuthenticationToken authenticationToken) throws AuthenticationException {
        logger.info("------------进入认证--------");
        //获取登录接口的token
        String name = (String) authenticationToken.getPrincipal();
        //通过名字去查数据库是否存在该用户
        Wrapper<UserModel> wrapper = new EntityWrapper<>();
        wrapper.eq(UserModel.NAME, name);
        List<UserModel> user = userService.selectList(wrapper);
        if (user.size() == 0) {
            return null;
        }
        if(user.size() > 1 || user.get(0).getStatus().equals(CommunityConstants.USER_STATUS_JY)){
            return null;
        }
        SimpleAuthenticationInfo authenticationInfo = new SimpleAuthenticationInfo(user.get(0).getName(), user.get(0).getPass(),
                ByteSource.Util.bytes(user.get(0).getCredentialsSalt()), getName());
        return authenticationInfo;
    }
}
